package com.lujianfei.netease_android_mvvm;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lujianfei.netease_android_mvvm.adapter.MainAdapter;
import com.lujianfei.netease_android_mvvm.component.DaggerMainActivityComponent;
import com.lujianfei.netease_android_mvvm.databinding.ActivityMainBinding;
import com.lujianfei.netease_android_mvvm.entity.DataBean;
import com.lujianfei.netease_android_mvvm.main.IMainMvvm;
import com.lujianfei.netease_android_mvvm.main.impl.MainViewModel;
import com.lujianfei.netease_android_mvvm.module.MainActivityModule;

import java.util.List;

import javax.inject.Inject;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

public class MainActivity extends AppCompatActivity implements IMainMvvm.View{
    @Inject
    MainViewModel mViewModel;
    @Inject
    ActivityMainBinding binding;
    @Inject
    View mActionBarView;

    MainAdapter mAdapter;
    View mItemCancel;
    MenuItem mItemEdit;
    MenuItem mItemSelectAll;
    MenuItem mItemUnSelectAll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerMainActivityComponent
                .builder()
                .mainActivityModule(new MainActivityModule(this))
                .build()
                .inject(this);
        mAdapter = new MainAdapter(mViewModel);
        mViewModel.uiState.observe(this, new Observer<MainViewModel.UiState>() {
            @Override
            public void onChanged(MainViewModel.UiState uiState) {
                Log.d("observe==",""+uiState);
                if(null != uiState.dataListList) {
                    update(uiState.dataListList);
                }
                if (null != uiState.selectAll) {
                    setSelectAll(uiState.selectAll);
                }
                if (null != uiState.title) {
                    setMiddleTitle(uiState.title);
                }
                if (null != uiState.onEditModeListener) {
                    uiState.onEditModeListener.onEditMode(isEditMode());
                }
                if (null != uiState.updateSelectAll) {
                    updateSelectAll(uiState.updateSelectAll);
                }
                if (null != uiState.toast) {
                    showToast(uiState.toast);
                }
                if (null != uiState.rangeChangedPosition) {
                    notifyItemRangeChanged(uiState.rangeChangedPosition.position,uiState.rangeChangedPosition.count);
                }
            }
        });
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerview.setAdapter(mAdapter);
        setSupportActionBar(binding.toolBar);
        ActionBar supportActionBar = getSupportActionBar();
        initView();
        supportActionBar.setCustomView(mActionBarView);
        supportActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        setMiddleTitle("Mvvm Demo");
        binding.setMainViewModel(mViewModel);
        mViewModel.load();
    }

    private void initView() {
        mItemCancel = mActionBarView.findViewById(R.id.menu_item_cancel);
        mItemCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEditMode(false);
            }
        });
    }

    @Override
    public void setMiddleTitle(String title) {
        TextView textView = mActionBarView.findViewById(R.id.tvTitle);
        textView.setText(title);
    }

    @Override
    public void update(List<DataBean> dataListList) {
        mAdapter.setData(dataListList);
    }

    @Override
    public void setEditMode(boolean editMode) {
        if (null != mAdapter) {
            mAdapter.setEditMode(editMode);
        }
        if (editMode) {
            mItemCancel.setVisibility(View.VISIBLE);
            mItemSelectAll.setVisible(true);
            mItemEdit.setVisible(false);
            setMiddleTitle(getResources().getQuantityString(R.plurals.selected,0,0));
            binding.setShowDelete(true);
        } else {
            mItemCancel.setVisibility(View.GONE);
            mItemSelectAll.setVisible(false);
            mItemUnSelectAll.setVisible(false);
            mItemEdit.setVisible(true);
            setMiddleTitle(getString(R.string.main_title));
            binding.setShowDelete(false);
        }
    }

    @Override
    public void setSelectAll(boolean selectAll) {
        mAdapter.setSelectAll(selectAll);
        updateSelectAll(selectAll);
    }

    @Override
    public void updateSelectAll(boolean selectAll) {
        if (selectAll) {
            mItemSelectAll.setVisible(false);
            mItemUnSelectAll.setVisible(true);
        } else {
            mItemSelectAll.setVisible(true);
            mItemUnSelectAll.setVisible(false);
        }
    }

    @Override
    public boolean isEditMode() {
        return mAdapter.isEditMode();
    }

    @Override
    public void notifyItemRangeChanged(int position, int count) {
        mAdapter.notifyItemRangeChanged(position,count);
    }

    @Override
    public AppCompatActivity getContext() {
        return this;
    }

    @Override
    public void notifyItemRemoved(int position) {
        mAdapter.notifyItemRemoved(position);
    }

    @Override
    public void notifyItemChanged(int position) {
        mAdapter.notifyItemChanged(position);
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_item_edit:
            {
                setEditMode(true);
            }
            break;
            case R.id.menu_item_selectAll:
            {
                mViewModel.setSelectAll(true);
            }
            break;
            case R.id.menu_item_unselectAll:
            {
                mViewModel.setSelectAll(false);
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.more,menu);
        mItemEdit = menu.findItem(R.id.menu_item_edit);
        mItemSelectAll = menu.findItem(R.id.menu_item_selectAll);
        mItemUnSelectAll = menu.findItem(R.id.menu_item_unselectAll);
        mItemSelectAll.setVisible(false);
        mItemUnSelectAll.setVisible(false);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        if (isEditMode()) {
            setEditMode(false);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
